package com.ljg.activiti.e_historyquery;

import java.util.List;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricVariableInstance;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 流程历史数据查询测试类
 * @author guo
 *
 */
public class HistoryQueryTest {
	
	private Logger log = LoggerFactory.getLogger(HistoryQueryTest.class);
	
	// 流程引擎对象
	private ProcessEngine processEngine;
	
	@Before
	public void before() {
		/**
		 * 创建默认的流程引擎对象，默认读取classpath下的activiti.cfg.xml创建流程引擎对象
		 */
		processEngine = ProcessEngines.getDefaultProcessEngine();
		log.info("创建流程引擎对象成功！");
	}
	
	/**
	 * 查询历史流程实例
	 */
	@Test
	public void findHistoryProcessInstance() {
		String processInstanceId = "25001"; // 流程实例Id
		HistoricProcessInstance hpi = processEngine.getHistoryService() // 与历史数据（历史表）相关的service
			.createHistoricProcessInstanceQuery() // 创建历史流程实例查询
			.processInstanceId(processInstanceId) // 使用流程实例Id查询
			.singleResult(); // 返回唯一结果集
		
		if(null == hpi) {
			log.error("没有查询到processInstanceId：" + processInstanceId + "对应的历史流程实例！");
			return;
		}
		
		log.info("流程实例Id：" + hpi.getId() + "，流程实例名称：" + hpi.getName() + "，流程定义Id：" + hpi.getProcessDefinitionId() + "，流程实例开始时间：" + hpi.getStartTime() + "，流程实例结束时间：" + hpi.getEndTime());
	}
	
	/**
	 * 查询历史活动
	 */
	@Test
	public void findHistoryActivities() {
		String processInstanceId = "25001";
		List<HistoricActivityInstance> list = processEngine.getHistoryService() // 与历史数据（历史表）相关的service
			.createHistoricActivityInstanceQuery() // 创建历史活动查询
			.processInstanceId(processInstanceId) // 流程实例Id
			.list();
		
		if(null == list || list.size() == 0) {
			log.error("没有查询到" + processInstanceId + "的历史活动！");
			return;
		}
		
		for(HistoricActivityInstance hai : list) {
			log.info(hai.getId() + "---" + hai.getProcessDefinitionId() + "---" + hai.getProcessInstanceId() + "---" + hai.getActivityId() + "---" + hai.getActivityName() + "---" + hai.getActivityType());
		}
			
	}
	
	/**
	 * 查询历史任务
	 */
	@Test
	public void findHistoryTask() {
		String assignee = "张三"; // 历史任务办理人
		List<HistoricTaskInstance> hTaskList = processEngine.getHistoryService() // 与历史数据（历史表）相关的service
			.createHistoricTaskInstanceQuery() // 创建历史任务实例查询
			.taskAssignee(assignee) // 指定历史任务的办理人
			.list();
		
		if(null == hTaskList || hTaskList.size() == 0) {
			log.error("没有查询到" + assignee + "的历史任务！");
			return;
		}
		
		for(HistoricTaskInstance ht : hTaskList) {
			log.info("任务Id=" + ht.getId() + "，任务名称=" + ht.getName() + "，流程实例Id=" + ht.getProcessInstanceId() + "，开始时间=" + ht.getStartTime() + "，结束时间=" + ht.getEndTime());
		}
	}
	
	/**
	 * 查询流程变量历史表
	 */
	@Test
	public void findHistoryVariables() {
		String processInstanceId = "37501";
		List<HistoricVariableInstance> hvList = processEngine.getHistoryService()//
			.createHistoricVariableInstanceQuery() // 创建历史的流程变量查询
			.processInstanceId(processInstanceId) // 流程实例Id
			.list();
		
		if(null == hvList || hvList.size() == 0) {
			log.error("没有查询到" + processInstanceId + "的历史变量！");
			return;
		}
		
		for(HistoricVariableInstance hvi : hvList) {
			log.info("" + hvi.getId() + "----" + hvi.getProcessInstanceId() + "-----" + hvi.getVariableName() + "----" + hvi.getValue());
		}
	}

}
